package com.sugon.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.sugon.dao.SpareDevDao;
import com.sugon.entity.SpareDevEntity;
import com.sugon.entity.SysMsgEntity;
import com.sugon.service.SpareDevService;
import com.sugon.service.SysMsgService;
import com.sugon.service.SysProcessInfoService;
import com.sugon.utils.ProcessInfoEnum;
import com.sugon.utils.SysMsgType;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

import static com.sugon.utils.ShiroUtils.getUserId;

/**
 * 备件设备关联Service实现类
 *
 * @author sugon
 * @date 2019-07-17 15:19:36
 */
@Service("spareDevService")
public class SpareDevServiceImpl implements SpareDevService {
    @Resource
    private SpareDevDao spareDevDao;
    @Resource
    private SysProcessInfoService sysProcessInfoService;
    @Resource
    private SysMsgService sysMsgService;

    /**
     * getMatchedSparePartIdList
     *
     * @param map
     * @return
     */
    @Override
    public List<String> getMatchedSparePartIdList(Map<String, Object> map) {
        List<Map<String, Object>> matchedSparePartIdMapList = spareDevDao.getMatchedSparePartIdList(map);
        if (CollectionUtil.isNotEmpty(matchedSparePartIdMapList)) {
            Set<String> matchedList = new TreeSet<>();
            for (Map<String, Object> matchedSparePartIdMap : matchedSparePartIdMapList) {
                String id = MapUtil.getStr(matchedSparePartIdMap, "ID");
                if (StrUtil.isNotEmpty(id)) {
                    matchedList.add(id);
                }
                String spareId = MapUtil.getStr(matchedSparePartIdMap, "SPARE_ID");
                if (StrUtil.isNotEmpty(spareId)) {
                    matchedList.add(spareId);
                }
                String spareRepId = MapUtil.getStr(matchedSparePartIdMap, "SPARE_REP_ID");
                if (StrUtil.isNotEmpty(spareRepId)) {
                    matchedList.add(spareRepId);
                }
            }
            // 再一次去除重复元素
            return new ArrayList<>(matchedList);
        }
        return null;
    }

    /**
     * queryObject
     *
     * @param id 主键
     * @return
     */
    @Override
    public SpareDevEntity queryObject(String id) {
        return spareDevDao.queryObject(id);
    }

    /**
     * queryList
     *
     * @param map 参数
     * @return
     */
    @Override
    public List<SpareDevEntity> queryList(Map<String, Object> map) {
        return spareDevDao.queryList(map);
    }

    /**
     * isAdded
     *
     * @param sparePartId    备件ID
     * @param repSparePartId 替换件ID
     * @return
     */
    @Override
    public boolean isAdded(String sparePartId, String repSparePartId) {
        Map<String, Object> param = MapUtil.newHashMap(2);
        param.put("spareId", sparePartId);
        param.put("spareRepId", repSparePartId);
        return queryTotal(param) > 0;
    }

    /**
     * queryTotal
     *
     * @param map 参数
     * @return
     */
    @Override
    public int queryTotal(Map<String, Object> map) {
        return spareDevDao.queryTotal(map);
    }

    /**
     * save
     *
     * @param spareDev 实体
     * @return
     */
    @Override
    public int save(SpareDevEntity spareDev) {
        spareDev.setId(RandomUtil.randomString(32));
        spareDev.setApplyUserId(getUserId());
        spareDev.setCheckFlag(0);
        int rows = spareDevDao.save(spareDev);
        if (rows > 0) {
            //添加审核 消息提醒
            sysProcessInfoService.saveProcessInfo(spareDev.getId(), ProcessInfoEnum.SPA_KTHJ, 0, null);
            sysMsgService.addApproveItem(ProcessInfoEnum.SPA_KTHJ, spareDev.getId(), null, 0);
        }
        return rows;
    }

    /**
     * update
     *
     * @param spareDev 实体
     * @return
     */
    @Override
    public int update(SpareDevEntity spareDev) {
        return spareDevDao.update(spareDev);
    }

    /**
     * review
     *
     * @param id
     * @param checkFlag
     * @return
     */
    @Override
    public int review(String id, String checkFlag) {
        SpareDevEntity spareDev = spareDevDao.queryObject(id);
        spareDev.setCheckUserId(getUserId());
        spareDev.setCheckFlag(Integer.valueOf(checkFlag));
        int rows = spareDevDao.update(spareDev);
        if (rows > 0) {
            //添加审核流程记录
            sysProcessInfoService.saveProcessInfo(spareDev.getId(), ProcessInfoEnum.SPA_KTHJ, spareDev.getCheckFlag(), null);
            if (spareDev.getCheckFlag().equals(1)) {
                // 保存关联记录
                SpareDevEntity repSpareDev = new SpareDevEntity();
                repSpareDev.setCheckFlag(1);
                repSpareDev.setSpareRepId(spareDev.getSpareId());
                repSpareDev.setSpareId(spareDev.getSpareRepId());
                repSpareDev.setApplyUserId(spareDev.getApplyUserId());
                repSpareDev.setCheckUserId(spareDev.getCheckUserId());
                repSpareDev.setId(RandomUtil.randomString(32));
                spareDevDao.save(repSpareDev);
                // 删除系统消息
                SysMsgEntity sysMsgEntity = new SysMsgEntity();
                sysMsgEntity.setDataId(spareDev.getId());
                sysMsgEntity.setDataType(ProcessInfoEnum.SPA_KTHJ);
                sysMsgEntity.setDataFlag(SysMsgType.DATA_DEL_END);
                sysMsgService.allMsgData(sysMsgEntity);
            }
        }
        return rows;
    }

    /**
     * delete
     *
     * @param id
     * @return
     */
    @Override
    public int delete(String id) {
        return spareDevDao.delete(id);
    }

    /**
     * deleteBatch
     *
     * @param ids
     * @return
     */
    @Override
    public int deleteBatch(String[] ids) {
        return spareDevDao.deleteBatch(ids);
    }
}
